package cn.com.gridinfo.sooc.core.course.classes.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.core.course.classes.bean.CourseKinds;
import cn.com.gridinfo.sooc.core.course.classes.bean.KindsGrid;
import cn.com.gridinfo.sooc.utils.widget.UnScrollListView;

/**
 * 分类1级目录
 * Created by yingchunli on 2016/5/9.
 */
public class KindsListAdapter extends BaseAdapter {

    private List<CourseKinds> cklist;
    private Context context;

    public KindsListAdapter(Context context, List<CourseKinds> cklist) {
        this.context = context;
        this.cklist = cklist;
    }

    @Override
    public int getCount() {
        return cklist.size();
    }

    @Override
    public Object getItem(int position) {
        return cklist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.kinds_list_item, parent, false);
        }
        //课程1级分类目录名称 和 图标
        TextView title = (TextView) convertView.findViewById(R.id.mc);
        title.setText(cklist.get(position).getXsm());
        ImageView title_icon = (ImageView) convertView.findViewById(R.id.mc_left_icon);

        //显示课程1级分类名称图标
        if (position % 4 == 0) {
            title_icon.setImageResource(R.mipmap.course_kinds_icon1);
        } else if (position % 4 == 1) {
            title_icon.setImageResource(R.mipmap.course_kinds_icon2);
        } else if (position % 4 == 2) {
            title_icon.setImageResource(R.mipmap.course_kinds_icon3);
        } else if (position % 4 == 3) {
            title_icon.setImageResource(R.mipmap.course_kinds_icon4);
        }

        //2级分类显示
        List<KindsGrid> kindsGridList = new ArrayList<>();
        for (int i = 0; i < cklist.get(position).getSub().size(); i += 2) {
            KindsGrid kindsGrid = new KindsGrid();

            kindsGrid.setCourseKindsLeft(cklist.get(position).getSub().get(i));
            if (i + 1 < cklist.get(position).getSub().size()) {
                kindsGrid.setCourseKindsRight(cklist.get(position).getSub().get(i + 1));
            } else {
                kindsGrid.setCourseKindsRight(new CourseKinds());
            }
            kindsGridList.add(kindsGrid);
        }

        UnScrollListView grid_list = (UnScrollListView) convertView.findViewById(R.id.grid_list);
        KindsGridAdapter adapter = new KindsGridAdapter(context, kindsGridList);
        grid_list.setAdapter(adapter);
        return convertView;
    }
}
